﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientAppViaWebService.BaseLogicServiceRef;
using ClientAppViaWebService.Utility;

namespace ClientAppViaWebService.Forms
{
    public partial class TObjectEditForm : Form
    {
        public enum eMode { mdEdit = 1, mdAdd };

        protected eMode mode;
        //private readonly Int32 ClassID = 1;
        private BaseLogicServiceRef.BaseLogicService baseLogicService;      

        #region Properties.

        public Int32 ID
        {
            get
            {
                if (idTextBox.Text.Length != 0)
                    return Int32.Parse(idTextBox.Text);
                return -1;
            }
            set { idTextBox.Text = value.ToString(); }
        }
        public String EXT_ID
        {
            get
            {
                if (extIdTextBox.Text.Length != 0)
                    return extIdTextBox.Text;
                return "-1";
            }
            set { extIdTextBox.Text = value.ToString(); }
        }
        public Int32 Class  
        {
            get
            {
                if (classTextBox.Text.Length != 0)
                    return Int32.Parse(classTextBox.Text);
                return -1;
            }
            set { classTextBox.Text = value.ToString(); }
        }
        public String ParentId
        {
            get
            {
                if (parentTextBox.Text.Length != 0)
                    return parentTextBox.Text;
                return "0";
            }
            set 
            {
                parentTextBox.Text = (value == null) ? "0" : value.ToString();
            }
        }
        public String ObjName
        {
            get { return nameTextBox.Text; }
            set { nameTextBox.Text = value; }
        }
        public String DataBaseName
        {
            get { return txtDataBaseName.Text; }
            set { txtDataBaseName.Text = value; }
        }

        #endregion
        
        #region Constructor.
        public TObjectEditForm()
        {
            InitializeComponent();
            baseLogicService = new BaseLogicService(); 
        }
        #endregion      
       
        public  void SetForm(TObject parentNode, eMode _mode, string dataBaseName)
        {
            LoadUpsertFormData (parentNode ,_mode, dataBaseName);
        }

        #region Private methods.

        private void LoadUpsertFormData(TObject parentNode,eMode _mode, string dataBaseName)
        {
            mode = _mode;
            DataBaseName = dataBaseName;

            if (mode == eMode.mdEdit)
            {
                LoadEditFormData(parentNode.ID);
            }
            else if (mode == eMode.mdAdd)
            {
                LoadInsertFormData(parentNode);
            }
        }

        private void LoadInsertFormData(TObject parentNode)
        {
            ParentId = parentNode.EXT_ID;
            //Class = ClassID;
         }

        #endregion

        #region Button handlers.      

        #region protected void okButton_Click(object sender, EventArgs e)
        protected virtual void okButton_Click(object sender, EventArgs e)
        {
            if (mode == eMode.mdEdit) UpdateEntity();
            if (mode == eMode.mdAdd) InsertEntity();

        }
        #endregion

        #region protected virtual void cancelButton_Click(object sender, EventArgs e)
        protected virtual void cancelButton_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #endregion


        protected virtual void SetFormProperties(Object _object)
        {
            TObject o = (_object as TObject);
            ID = o.ID;
            EXT_ID = o.EXT_ID;
            ParentId = o.PARENT_ID;
            Class = o.CLASS_ID;
            ObjName = o.NAME;
        }

        protected virtual Object RetrieveFormProperties()
        {
            TObject obj = new TObject();
            obj.ID = ID;
            obj.EXT_ID = EXT_ID;
            obj.PARENT_ID = ParentId;
            obj.CLASS_ID = Class;
            obj.NAME = ObjName;
            obj.DataBaseName = DataBaseName;

            return obj;
        }

        protected virtual void LoadEditFormData(int parentNodeID)
        {
            TObject p = baseLogicService.SelectTObject_By_DataBase_And_Id(DataBaseName, parentNodeID);
            SetFormProperties(p);
        }  

        protected virtual void UpdateEntity()
        {
            TObject obj = RetrieveFormProperties() as TObject;
            baseLogicService.UpdateTobject(obj);
        }

        protected virtual void InsertEntity()
        {
            TObject obj = RetrieveFormProperties() as TObject;
            baseLogicService.InsertTobject(obj);
        }

       
    }
}
